package com.yanqu.road.logic.xlogic.rank.season.stage;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.entity.enums.eSeasonRewardType;
import com.yanqu.road.entity.rank.xrank.XSeasonStageUserRank;
import com.yanqu.road.logic.bussiness.rank.XRankBussiness;
import com.yanqu.road.logic.sort.*;
import com.yanqu.road.logic.xlogic.base.BaseXMap;
import com.yanqu.road.logic.xlogic.base.IChannelSeasonMaxRank;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 个人赛季阶段榜
 */
public class XSeasonStageUserRankMap extends BaseXMap {

    private IChannelSeasonMaxRank maxRank;

    public XSeasonStageUserRankMap(String dbName, String tableName, IChannelSeasonMaxRank maxRank) {
        super(dbName, tableName);
        this.maxRank = maxRank;
    }

    /**
     * 阶段榜单    （系统ID，赛季ID，类型，阶段，范围，数据）
     */
    private final Map<Integer, Map<Integer, Map<Integer, Map<Integer, Map<String, XSeasonStageUserRankListModel>>>>> userRankMap = new ConcurrentHashMap<>();

    /**
     * 拿榜单
     */
    public XSeasonStageUserRankListModel getUserRankListModel(int systemId, int seasonId, int type, int stage, String rangeKey) {
        synchronized (userRankMap) {
            userRankMap.putIfAbsent(systemId, new ConcurrentHashMap<>());
            userRankMap.get(systemId).putIfAbsent(seasonId, new ConcurrentHashMap<>());
            userRankMap.get(systemId).get(seasonId).putIfAbsent(type, new ConcurrentHashMap<>());
            userRankMap.get(systemId).get(seasonId).get(type).putIfAbsent(stage, new ConcurrentHashMap<>());
            Map<String, XSeasonStageUserRankListModel> map = userRankMap.get(systemId).get(seasonId).get(type).get(stage);
            if (!map.containsKey(rangeKey)) {
                // 读库
                List<XSeasonStageUserRank> rankList = XRankBussiness.getXSeasonStageUserRankList(dbName, tableName, systemId, seasonId, type, stage, rangeKey);
                XSeasonStageUserRankListModel model = null;
                if (type == eSeasonRewardType.WasheScoreRank.getIntValue()) {
                    model = new XSeasonStageUserRankListModel(dbName, tableName, systemId, seasonId, type, stage, rangeKey, maxRank.getChannelSeasonMaxRank(systemId, seasonId, type, rangeKey), new XSeasonUserStageRankSort());
                } else if (type == eSeasonRewardType.JinBiaoScoreRank.getIntValue()) {
                    model = new XSeasonStageUserRankListModel(dbName, tableName, systemId, seasonId, type, stage, rangeKey, maxRank.getChannelSeasonMaxRank(systemId, seasonId, type, rangeKey), new XSeasonJinBiaoScoreStageRankSort());
                } else if (type == eSeasonRewardType.JinBiaoPeakRank.getIntValue()) {
                    model = new XSeasonStageUserRankListModel(dbName, tableName, systemId, seasonId, type, stage, rangeKey, maxRank.getChannelSeasonMaxRank(systemId, seasonId, type, rangeKey), new XSeasonJinBiaoPeakStageRankSort());
                }
                model.initByRankList(new LinkedList<>(rankList));
                map.put(rangeKey, model);
            }
            return map.get(rangeKey);
        }
    }

    public void addXSeasonStageUserRankListModel(int systemId, int seasonId, int type, int stage, String rangeKey, LinkedList<XSeasonStageUserRank> rankList) {
        int maxRank = this.maxRank.getChannelSeasonMaxRank(systemId, seasonId, type, rangeKey);
        XSeasonStageUserRankListModel model = new XSeasonStageUserRankListModel(dbName, tableName, systemId, seasonId, type, stage, rangeKey, rankList, maxRank, new XSeasonJinBiaoScoreStageRankSort());
        model.addAllDbRank(maxRank);
        synchronized (userRankMap) {
            userRankMap.putIfAbsent(systemId, new ConcurrentHashMap<>());
            userRankMap.get(systemId).putIfAbsent(seasonId, new ConcurrentHashMap<>());
            userRankMap.get(systemId).get(seasonId).putIfAbsent(type, new ConcurrentHashMap<>());
            userRankMap.get(systemId).get(seasonId).get(type).putIfAbsent(stage, new ConcurrentHashMap<>());
            userRankMap.get(systemId).get(seasonId).get(type).get(stage).put(rangeKey, model);
        }
    }


    /**
     * 获取全部榜单
     */
    public Map<Integer, Map<Integer, Map<Integer, Map<Integer, Map<String, XSeasonStageUserRankListModel>>>>> getUserRankMap() {
        return userRankMap;
    }



    @Override
    public void save() {
        for (Map<Integer, Map<Integer, Map<Integer, Map<String, XSeasonStageUserRankListModel>>>> value : getUserRankMap().values()) {
            for (Map<Integer, Map<Integer, Map<String, XSeasonStageUserRankListModel>>> value1 : value.values()) {
                for (Map<Integer, Map<String, XSeasonStageUserRankListModel>> value2 : value1.values()) {
                    for (Map<String, XSeasonStageUserRankListModel> value3 : value2.values()) {
                        for (XSeasonStageUserRankListModel model : value3.values()) {
                            model.save();
                        }
                    }
                }
            }
        }
    }

    @Override
    public void loadDb(Object param) {

    }
}
